#ifndef _MAINRUN_H_ 
#define _MAINRUN_H_

#include "stdafx.h"
#include "plugins.h"

class CMainRun :	public IObjectRun,
					public CUnknownImp
{
public:
	CMainRun(void);
	virtual ~CMainRun(void);

	BEGIN_STDCOM_MAP
		STDCOM_INTERFACE_ENTRY_UNKNOWN_(IObjectRun)
		STDCOM_INTERFACE_ENTRY(IObjectRun)
	END_STDCOM_MAP

public:
	//IObjectRun
	std_method(RegisterCode)(LPCSTR buf, ULONG ulen);
	std_method_(LPCSTR, GetRegisterCode)();
	std_method(SetPath)(LPSTRING run, UINT len);
	std_method_(LPSTRING, GetPath)();
	std_method(Init)(_pinstance instance, basic_tchar* argv[], int argc);
	std_method(Start)(_pinstance instance, UINT type);
	std_method(Stop)(_pinstance instance, UINT type,UINT exit);
	std_method_(UINT, Run)(_pinstance instance, UINT type);
	std_method(NotifyExit)(_pinstance instance, UINT exit);
	std_method(Uninit)(_pinstance hInstance);
	std_method(Clear)();
	std_method(GetRot)(IBase** Base);
	std_method(GetObjectLoader)(IBase** Base);
	std_method(AddObject)(REFCLSID clsid, LPCSTR progId, IBase* pBase, UINT Id, LPCSTR Component, UINT type);
	std_method(DelObject)(REFCLSID clsid, LPCSTR progId, IBase* pBase, UINT Id, LPCSTR Component, UINT type);
	std_method_(CLSID, GetRunPluginCLSID)();
	std_method_(ULONG, GetRunPluginsCount)();
	std_method_(int, GetArgc)();
	std_method_(basic_tchar*, GetIndexArgv)(int index);
	std_method_(ULONG, GetRunIndex)(ULONG index);
	std_method_(UINT, GetExitCode)();

private:
	std_method(RegisterRunArgc)(int iArgc, basic_tchar* Argv[]);
	std_method(RegisterPlugins)(LPCSTR Buf);
	std_method(RegisterPluginClsid)();
	std_method(RegisterPluginComponents)();
	std_method(RegisterRotAndlibManager)();
	std_method(RegisterContainer)(LPCSTR name);

private:
	std_method(CreatePlugin)(REFCLSID iid, UINT index);
	std_method(UninitAllPlugins)();
	std_method(CreateRotPlugin)(REFCLSID iid, IBase **ppunk, UINT index);
	std_method(DestroyAllRotPlugin)();
	std_method(DestroyRotPlugin)(REFCLSID iid);

private:
	std_method_(UINT, DoPluginsRun)(int iArgc, basic_tchar* Argv[], UINT uType);
	std_method(DoPluginsStartFunc)(int iArgc, basic_tchar* Argv[], UINT uType);
	std_method(DoPluginsStopFunc)(UINT uType, UINT uExit);
	std_method_(UINT, GetIExitCode)(CLSID iid);

private:
	std_method(RunPlugin_Start)(_pinstance hInstance, BaseConstPtr pBase, UINT uType);
	std_method(RunPlugin_Stop)(_pinstance hInstance, BaseConstPtr pBase, UINT uExit);
	std_method(RunStdPlugin_Start)(_pinstance hInstance, BaseConstPtr pBase, UINT uType);
	std_method(RunStdPlugin_Stop)(_pinstance hInstance, BaseConstPtr pBase, UINT uExit);
	std_method(RunMsgPlugin_Start)(BaseConstPtr pBase);
	std_method(RunMsgPlugin_Stop)(BaseConstPtr pBase);

private:
	std_method(Reset)();
	std_method_(basic_tstring, ComponentPath)(LPCSTR ComponentName);

private:
	_pinstance							m_hInstance;
	RotPtr								m_pRot;
	LibManagerPtr						m_plibManager;
private:
	basic_tstring						m_strRunPath;
	string								m_strRegisterCode;
private:
	CPlugins							m_Plugins;
	PluginVector						m_PluginsClsid;
	PluginVector						m_PluginsDynamicClsid;
	ComponentPlugin						m_PluginsComponents;
	CLSID								m_RunPlugin;
	basic_tstring						m_strContainer;
	basic_tchar**						m_argv;
	int									m_argc;
};

#endif
